<template>
	<view>
		<view class='search acea-row row-between-wrapper'>
			<view class='input acea-row row-between-wrapper'>
				<text class='iconfont icon-sousuo2'></text>
				<input type='text' :value='searchValue' :focus="focus" placeholder='请输入患者名称'
					placeholder-class='placeholder' @input="setValue"></input>
			</view>
			<view class='bnt' @tap='searchBut'>搜索</view>
		</view>
		<view class="nav">
			<uni-segmented-control ref="segRef" :current="current" :values="items" :style-type="styleType"
				:active-color="activeColor" @clickItem="onClickItem" />
		</view>
		<view class="content">
			<view v-if="current === 0">
				<view class="asking-box" v-if="askingList.length>0">
					<view class="list-item" v-for="(item,index) in askingList">
						<view class="doctor">
							<view class="info">
								医&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp生：{{item.doctor_name}}
							</view>
							<view class="money">￥{{item.pay_price}}</view>
						</view>
						<view class="p-title">患者信息:</view>
						<view class="patient">
							<view class="avatar">
								<image :src="item.avatar" mode="aspectFill"></image>
							</view>
							<view class="right-box">
								<view class="top-box">
									<view class="name">姓&nbsp&nbsp&nbsp名：{{item.patient_info.name}}</view>
									<view class="sex" v-if="item.patient_info.sex == 1">女</view>
									<view class="sex" v-else>男</view>
									<view class="age">{{item.patient_info.age}}岁</view>
								</view>
								<view class="phone item-space">手机号：{{item.patient_info.phone}}</view>
								<view class="idcard item-space">身份证：{{item.patient_info.idcard}}</view>
								<view class="address item-space">地&nbsp&nbsp&nbsp址：{{item.patient_info.address}}</view>
							</view>
						</view>
						<view class="footer">
							<view class="time">预约时间：{{item.create_time}}</view>
							<view class="btn" @click="doVerify(item.id)">核销</view>
						</view>
					</view>
					<view class='loadingicon acea-row row-center-wrapper' @click="clickMore(false)">
						<text class='loading iconfont icon-jiazai'
							:hidden='askingLoading==false'></text>{{askingLoadTitle}}
					</view>
				</view>
				<view class="nodata" v-else>
					<emptyPage title="暂无数据～"></emptyPage>
				</view>
			</view>
			<view v-if="current === 1">
				<view class="finish-box" v-if="finishList.length>0">
					<view class="list-item" v-for="(item,index) in finishList">
						<view class="doctor">
							<view class="info">
								医&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp生：{{item.doctor_name}}
							</view>
							<view class="money">￥{{item.pay_price}}</view>
						</view>
						<view class="p-title">患者信息:</view>
						<view class="patient">
							<view class="avatar">
								<image :src="item.avatar" mode="aspectFill"></image>
							</view>
							<view class="right-box">
								<view class="top-box">
									<view class="name">姓&nbsp&nbsp&nbsp名：{{item.patient_info.name}}</view>
									<view class="sex" v-if="item.patient_info.sex == 1">女</view>
									<view class="sex" v-else>男</view>
									<view class="age">{{item.patient_info.age}}岁</view>
								</view>
								<view class="phone item-space">手机号：{{item.patient_info.phone}}</view>
								<view class="idcard item-space">身份证：{{item.patient_info.idcard}}</view>
								<view class="address item-space">地&nbsp&nbsp&nbsp址：{{item.patient_info.address}}</view>
							</view>
						</view>
						<view class="footer">
							<view class="time">预约时间：{{item.create_time}}</view>
						</view>
					</view>
					<view class='loadingicon acea-row row-center-wrapper' @click="clickMore(false)">
						<text class='loading iconfont icon-jiazai'
							:hidden='finishLoading==false'></text>{{finishLoadTitle}}
					</view>
				</view>
				<view class="nodata" v-else>
					<emptyPage title="暂无数据～"></emptyPage>
				</view>
			</view>
		</view>

		<view></view>
	</view>
</template>

<script>
	import emptyPage from '@/components/emptyPage.vue';
	
	import {
		docAppointmentList,
		docAppointmentVerify
	} from '@/api/doc_appointment.js';
	import {
		mapGetters
	} from "vuex";
	export default {
		components: {
			emptyPage
		},
		computed: {
			...mapGetters(['userInfo', 'uid','routeError'])
		},
		data() {
			return {
				searchValue: '',
				focus: false,

				items: ['待就诊', '已结束'],
				current: 0,
				activeColor: '#38ce8c',
				styleType: 'text',

				askingLoadTitle: '加载更多',
				askingLoading: false,
				askingLoadend: false,
				askingWhere: {
					'page': 1,
					'limit': 20,
					'name':''
				},
				askingList: [],
				finishLoadTitle: '加载更多',
				finishLoading: false,
				finishLoadend: false,
				finishWhere: {
					'page': 1,
					'limit': 20,
					'name':''
				},
				finishList: [],
				isSearch: false,
			}
		},
		onLoad() {
			if(this.routeError) {
				this.$store.commit('GO_ERROR');
				return
			}
			this.getAskingList();
		},
		onPullDownRefresh() {
			console.log('rk==>refresh')
			this.clickMore(true);
		},
		onReachBottom() {
			this.clickMore(false);
		},

		methods: {

			setValue: function(event) {
				this.$set(this, 'searchValue', event.detail.value);

				this.askingWhere.name = this.searchValue;
				this.finishWhere.name = this.searchValue;

				console.log('rk==>[...]' + JSON.stringify(this.searchValue));
				// 清空自动搜索
				if (this.searchValue == '') {
					this.searchBut();
				}
			},
			searchBut: function() {
				this.isSearch = true;
				this.onClickItem(this.$refs.segRef);
			},

			onClickItem(e) {

				console.log("rk===>[e]" + e.currentIndex);

				if (this.current !== e.currentIndex || this.isSearch) {
					this.current = e.currentIndex
					uni.stopPullDownRefresh();

					if (this.current == 1 ) {
						this.isSearch = false;
						this.finishLoadend = false;
						this.finishWhere.page = 1;
						this.getFinishList();
					}

					if (this.current == 0 ) {
						this.isSearch = false;
						this.askingLoadend = false;
						this.askingWhere.page = 1;
						this.getAskingList();
					}

				}
			},
			/**
			 * 更多
			 */
			clickMore(isRefresh) {

				if (this.current == 1) {

					if (this.finishLoadend && !isRefresh) {
						uni.stopPullDownRefresh();
						return
					}
					this.finishLoadend = false;
					if (isRefresh) {
						this.finishWhere.page = 1;
					}
					this.getFinishList();

				} else {
					if (this.askingLoadend && !isRefresh) {
						uni.stopPullDownRefresh();
						return
					}
					this.askingLoadend = false;
					if (isRefresh) {
						this.askingWhere.page = 1;
					}
					this.getAskingList();
				}
			},
			/**
			 * 进行中
			 */
			getAskingList() {
				let that = this;

				that.askingWhere.uid = this.uid;
				//进行中
				that.askingWhere.status = 1;

				docAppointmentList(that.askingWhere).then(res => {
					uni.stopPullDownRefresh();

					if (that.askingWhere.page == 1) {
						that.askingList = [];
					}
					let resList = res.data.list;
					let allCount = res.data.count;
					that.askingList = that.$util.SplitArray(resList, that.askingList);
					that.$set(that, 'askingList', that.askingList);
					let loadend = that.askingList.length >= allCount;

					console.log('rk==>[xx]' + allCount + '[xx]' + that.askingList.length + '[bool]' + loadend);

					that.askingLoadend = loadend;
					that.askingLoadTitle = loadend ? '我也是有底线的' : '加载更多';
					that.askingWhere.page += 1;
					that.askingLoading = false;
				});

			},
			/**
			 * 已完成
			 */
			getFinishList() {
				let that = this;
				that.finishWhere.uid = this.uid;
				// 已完成
				that.finishWhere.status = 2;
				docAppointmentList(that.finishWhere).then(res => {
					uni.stopPullDownRefresh();

					if (that.finishWhere.page == 1) {
						that.finishList = [];
					}
					let resList = res.data.list;
					let allCount = res.data.count;
					that.finishList = that.$util.SplitArray(resList, that.finishList);
					that.$set(that, 'finishList', that.finishList);
					let loadend = that.finishList.length >= allCount;

					console.log('rk==>[xx]' + allCount + '[xx]' + that.finishList.length + '[bool]' + loadend);

					that.finishLoadend = loadend;
					that.finishLoadTitle = loadend ? '我也是有底线的' : '加载更多';
					that.finishWhere.page += 1;
					that.finishLoading = false;
				});

			},
			/**
			 * 核销
			 */
			doVerify(id){
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确定核销预约?',
					success: function(res) {
						if (res.confirm) {
							that.sureVerify(id);
						} else if (res.cancel) {
							console.log('fouding')
						}
					}
				});
			},
			sureVerify(id){
				docAppointmentVerify({id:id}).then(res => {
					if(res.status == 200){
						this.askingLoadend = false;
						this.askingWhere.page = 1;
						this.getAskingList();
					}
				}).catch(res => {
					this.$util.Tips({title: res.message});
				});
			}
			

		}
	}
</script>

<style lang="scss">
	page {}

	.search {
		padding-left: 30rpx;
	}

	.search {
		padding: 20rpx 0;
		background-color: #fff;
	}

	.search .input {
		width: 588rpx;
		background-color: #f7f7f7;
		border-radius: 33rpx;
		padding: 0 35rpx;
		box-sizing: border-box;
		height: 66rpx;
		margin-left: 30rpx;
	}

	.search .input input {
		width: 472rpx;
		font-size: 28rpx;
	}

	.search .input .placeholder {
		color: #bbb;
	}

	.search .input .iconfont {
		color: #000;
		font-size: 35rpx;
	}

	.search .bnt {
		width: 120rpx;
		text-align: center;
		height: 66rpx;
		line-height: 66rpx;
		font-size: 30rpx;
		color: #282828;
	}

	.nav {
		background-color: #fff;
	}

	.content {
		margin: 20rpx 0rpx;

		.list-item {
			background-color: #fff;
			margin: 20rpx 0;
			border-radius: 0rpx;
			padding: 30rpx 30rpx 0rpx;
		}
		.doctor {
			display: flex;
			justify-content: space-between;
			.money{
				color: #ff0000;
			}
		}
		.p-title{
			padding: 15rpx 0 0;
		}
		.patient {
			margin-top: 15rpx;
			display: flex;
			.avatar {
				width: 80rpx;
				height: 80rpx;
				border-radius: 80rpx;
				overflow: hidden;
				image {
					width: 100%;
					height: 100%;
				}
			}
			.right-box {
				margin-left: 15rpx;
				flex: 1;
				.top-box {
					display: flex;
					.sex {
						margin-left: 20rpx;
					}
					.age {
						margin-left: 20rpx;
					}
				}
				.item-space {
					padding: 6rpx 0 0;
				}
			}
		}
		.footer {
			margin-top: 15rpx;
			height: 80rpx;
			border-top: 2rpx solid #eee;
			display: flex;
			justify-content: space-between;
			align-items: center;
			.btn {
				background-color: #0079FE;
				color: #fff;
				padding: 6rpx 15rpx;
				border-radius: 10rpx;
			}
		}

		
	}
</style>
